package com.kltn.zigo.data.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.ListView;

import com.kltn.zigo.data.IP;
import com.kltn.zigo.process.JSONParser;

public class GroupCustomerLoader {
	JSONParser jParser = new JSONParser(); 
    
	ListView list;
	
	private static String url_get_group_customer = IP.get_IP() +  IP.get_path_service() + "get_group_customer.php";
 // JSON Node names
    public final String TAG_SUCCESS = "success";
    public final String TAG_GROUP_CUSTOMER = "group_customer";
    public final String TAG_GROUP_CUSTOMER_ID = "id";
    public final String TAG_GROUP_CUSTOMER_NAME = "name";
    
    // products JSONArray
    JSONArray groupcustomers = null;
    
    public ArrayList<HashMap<String, String>> GetGroupCustomer()
    {
    	ArrayList<HashMap<String, String>> groupcustomersList = new ArrayList<HashMap<String,String>>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_get_group_customer, "GET", params);

        Log.d("All group_customers: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
            	groupcustomers = json.getJSONArray(TAG_GROUP_CUSTOMER);

                // looping through All Products
                for (int i = 0; i < groupcustomers.length(); i++) {
                    JSONObject c = groupcustomers.getJSONObject(i);

                    // Storing each json item in variable
                    String group_customerID = c.getString(TAG_GROUP_CUSTOMER_ID);
                    String group_customerName = c.getString(TAG_GROUP_CUSTOMER_NAME);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_GROUP_CUSTOMER_ID, group_customerID);
                    map.put(TAG_GROUP_CUSTOMER_NAME, group_customerName);

                    // adding HashList to ArrayList
                    groupcustomersList.add(map);
                }
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return groupcustomersList;
    }
}
